<?php

declare(strict_types=1);

namespace App\Controller\Home\Drp;


use App\Model\Coupon;
use App\Model\CouponRecord;
use App\Model\Device;
use App\Model\Station;
use App\Model\User;
use App\Model\White;
use Hyperf\DbConnection\Db;
use Hyperf\Redis\Redis;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Trait\ResponseMsg;
use App\Model\Normal;
use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;
use App\Model\Distributors;

class WhiteController extends AbstractController
{
    use ResponseMsg;

    protected Redis $redis;
    protected Normal $normal;

    #[Inject]
    protected Distributors $distributors;

    #[Inject]
    protected Device $device;


    #[Inject]
    protected Coupon $coupon;

    public function __construct()
    {
        $this->redis = container()->get(Redis::class);
        $this->normal = container()->get(Normal::class);
    }

    /**白名单审核列表
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function checkList(RequestInterface $request): ResponseInterface
    {
        $info = $this->distributors->getInfo($request);
        if (!$info) {
            return $this->error(code: '400', msg: '登录态失效 请重新登录');
        }
        $is_check = $request->input('is_check');
        $where['distributor_id'] = $info['id'];
        if ($is_check != '') {
            $where['is_check'] = $is_check;
        }
        $list = White::where($where)
            ->with(['user' => function ($query) {
                $query->select('id', 'nickname','avatar','white_name');
            }])
            ->with(['station' => function ($query) {
                $query->select('id', 'name');
            }])
            ->get();

        return $this->success($list);
    }

    /**白名单添加
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function checkWhite(RequestInterface $request): ResponseInterface
    {
        $info = $this->distributors->getInfo($request);
        if (!$info) {
            return $this->error(code: '400', msg: '登录态失效 请重新登录');
        }
        $id = $request->input('id');
        $is_check = $request->input('is_check');
        if (!$id) {
            return $this->error(code: '1001', msg: '参数错误');
        }
        try {
            $whiteModel = White::where('id', $id)->first();
            $whiteModel->is_check = $is_check;
            $whiteModel->save();
            $keys = 'WHITE_DEVICE_' . $whiteModel->station_id;
            if ($is_check == 1) {
                $this->redis->sAdd($keys, $whiteModel->user_id);
            }
            return $this->success();
        } catch (\Exception $e) {
            Normal::logger($e->getMessage() . $e->getTraceAsString(), 'home');
            return $this->error(1001, $e->getMessage());
        }
    }

    /**电站白名单列表
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function whiteList(RequestInterface $request): ResponseInterface
    {
        try {
            $info = $this->distributors->getInfo($request);
            if (!$info) {
                return $this->error(code: '400', msg: '登录态失效 请重新登录');
            }
            $stations = Station::where('distributor_id', $info['id'])
                ->get(['id','name']);
            foreach ($stations as $k => $v) {
                $keys = 'WHITE_DEVICE_' . $v['id'];
                $users = redis()->sMembers($keys);
                $stations[$k]['white_users'] = User::whereIn('id', $users)->get(['id', 'nickname', 'avatar','white_name']);
            }
            return $this->success($stations);
        } catch (\Exception $e) {
            Normal::logger($e->getMessage() . $e->getTraceAsString(), 'home');
            return $this->error(1001, $e->getMessage());
        }
    }

    /**为设备移除白名单
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function delWhite(RequestInterface $request): ResponseInterface
    {
        $info = $this->distributors->getInfo($request);
        if (!$info) {
            return $this->error(code: '400', msg: '登录态失效 请重新登录');
        }
        $stationId = $request->input('station_id');
        $userId = $request->input('user_id');
        $keys = 'WHITE_DEVICE_' . $stationId;
        redis()->sRem($keys, $userId);
        White::where('station_id', $stationId)
            ->where('user_id', $userId)
            ->delete();
        return $this->success();
    }
}
